/**
 * long description for the file
 *
 * @summary 公共方法
 * @author wu[308822989@qq.com]
 *
 * Created at     : 2018-12-13 16:38:11
 * Last modified  : 2019-01-15 17:07:59
 */

//  遍历树
// import weixinSdk from 'weixin-js-sdk';// 微信js授权
export const dgTree = function (tree, fn, child = 'child') {
    for (var i = 0; i < tree.length; i++) {
        fn(tree[i]);
        if (tree[i][child] && tree[i][child].length > 0) {
            dgTree(tree[i][child], fn);
        }
    }
};

/**
 * 数组生成树
 * @param {Array} items 数据集
 * @param {*} select 需要返回的顶级目录ID
 * @param {String} id ID
 * @param {String} pid 上级ID
 * @param {String} child 生成树节点名
 * @returns {Object} res 生成好的树形对象
 */
export const buildTree = function (items = [], select, id = 'id', pid = 'pid', child = 'child') {
    let res = null;
    if (items.length < 1 || select === undefined) {
        return res;
    }
    //
    let tree = {};
    //
    for (let item of items) {
        if (!(item[id] in tree)) {
            tree[item[id]] = {};
        }
        //   压入当前数据
        tree[item[id]] = Object.assign({}, tree[item[id]], item);
        //   添加树属性
        if (!(child in tree[item[id]])) tree[item[id]][child] = [];
        //   找
        if (tree[item[pid]]) {
            tree[item[pid]][child].push(tree[item[id]]);
        } else {
            tree[item[pid]] = {};
            tree[item[pid]][child] = [tree[item[id]]];
        }
    }
    //
    res = tree[select];
    if (!(id in res)) {
        //  顶级目录 没有名称 添加名称
        res[id] = select;
    }
    return res;
};

// 获取URL参数
export const getQueryString = (paramname, url) => {
    var reg = new RegExp('(^|&)' + paramname + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return decodeURIComponent(r[2]);
    return null;
};
// 字符串转时间格式
export const strToDate = (objDate) => {
    /// <summary>字符串转换时间格式</summary>
    /// <param name="objDate" type="string or number">要转化的日期(可为能转换成日期格式的字符串、数字)</param>
    try {
        var date = null;
        switch (typeof objDate) {
        case 'object' :
            date = objDate;
            break;
        case 'number':
            date = new Date(objDate);
            break;
        case 'string':
            objDate = objDate.replace('/Date(', '').replace(')/', '').replace(/-/g, '/').replace(/T/g, ' ');
            var re = /^[1-9]+[0-9]*]*$/;
            if (re.test(objDate)) {
                date = new Date(parseInt(objDate));
            } else {
                var isSafari = navigator.userAgent.indexOf('Safari');
                if (isSafari && objDate.indexOf('.') > 0) {
                    date = new Date(objDate.substr(0, objDate.indexOf('.')));
                    var milliseconds = objDate.substr(objDate.indexOf('.') + 1);
                    if (trim(milliseconds).length > 0) {
                        date.setMilliseconds(parseInt(milliseconds));
                    }
                } else {
                    date = new Date(objDate);
                }
            }
            break;
        default:
            break;
        }
        return date;
    } catch (ex) {
        return null;
    }
};
// 去除两侧空格
export const trim = (str) => {
    if (this.isNullOrWhiteSpace(str)) {
        return '';
    }
    if (typeof str !== 'object') {
        return str;
    }
    return str.replace(/(^\s*)|(\s*$)/g, '');
};

export const wxShare = (option) => {
    return new Promise((resolve, reject) => {
        let opt = Object.assign({
            title: '阿比达', // 分享标题
            desc: '', // 分享描述
            link: '', // 分享链接
            imgUrl: '', // 分享图标
            type: 'video或link', // 分享类型,music、video或link，不填默认为link
            dataUrl: '' // 如果type是music或video，则要提供数据链接，默认为空
        }, option);
        opt.success = () => {
            option.success();
            resolve('success');
        }; // 用户确认分享后执行的回调函数
        opt.cancel = () => {
            option.cancel();
            resolve('cancel');
        }; // 用户取消分享后执行的回调函数
        weixinSdk.onMenuShareAppMessage(opt);
    });
};
